package com.wash.shoes.domain.mtbusy;

import com.alibaba.fastjson.JSONObject;
import com.lop.open.api.sdk.DefaultDomainApiClient;
import com.lop.open.api.sdk.LopException;
import com.lop.open.api.sdk.domain.ECAP.CommonModifyCancelOrderApi.commonCancelOrderV1.CommonOrderCancelRequest;
import com.lop.open.api.sdk.plugin.LopPlugin;
import com.lop.open.api.sdk.plugin.factory.OAuth2PluginFactory;
import com.lop.open.api.sdk.request.ECAP.EcapV1OrdersCancelLopRequest;
import com.lop.open.api.sdk.response.ECAP.EcapV1OrdersCancelLopResponse;
import com.wash.shoes.common.enums.JdEnum;
import com.wash.shoes.common.enums.OrderStatusEnum;
import com.wash.shoes.domain.vo.MtPushMessage;
import com.wash.shoes.entity.JdCallRecordPo;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.entity.OrderTransportPo;
import com.wash.shoes.exception.WashException;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

/**
 * @author lijianguo
 * @Date 16/09/2024
 * 取消预定
 **/
@Log4j2
@Component
public class Mt5810001Busy extends AbstractMtCallback {

    @Override
    public MtCallbackEnum getType() {
        return MtCallbackEnum.MT_5810001;
    }

    @Override
    public Integer processBusy(MtPushMessage message, String token, String developerId) {
        JSONObject jsonObject = JSONObject.parseObject(message.getMessage());
        // 获取mt 预约单的id
        String mtOrderId = jsonObject.getString("orderId");
        OrderPo orderPo = orderService.getByMtOrderId(mtOrderId);
        if (orderPo == null) {
            throw new WashException("订单未入库,请联系管理员");
        }
        OrderTransportPo orderTransportPo = orderTransportService.getOneByOrderId(orderPo.getId());
        if (Integer.parseInt(orderTransportPo.getStatus()) >= JdEnum.ORDER_YI_LS.getStatus()) {
            throw new WashException("订单已经揽收,不能取消，亲联系管理员");
        }

        EcapV1OrdersCancelLopRequest cancelLopRequest = new EcapV1OrdersCancelLopRequest();
        DefaultDomainApiClient client = new DefaultDomainApiClient(jdProperty.getUrl(), 5000, 15000);
        LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdProperty.getAppKey(), jdProperty.getAppSecret(), jdProperty.getAccessToken());
        cancelLopRequest.addLopPlugin(lopPlugin);

        CommonOrderCancelRequest commonOrderCancelRequest = new CommonOrderCancelRequest();
        commonOrderCancelRequest.setOrderOrigin(Integer.parseInt(orderTransportPo.getOrderOrigin()));
        commonOrderCancelRequest.setCancelReason("用户取消");
        // 取消原因编码；枚举值：1-用户发起取消；2-超时未支付
        commonOrderCancelRequest.setCancelReasonCode("1");
        commonOrderCancelRequest.setCustomerCode(jdProperty.getCustomerCode());
        commonOrderCancelRequest.setWaybillCode(orderTransportPo.getWayBillCode());

        cancelLopRequest.setRequest(commonOrderCancelRequest);

        try {
            EcapV1OrdersCancelLopResponse response = client.execute(cancelLopRequest);
            log.info("EcapV1OrdersCancelLopResponse is request:[{}]  response:[{}]", JSONObject.toJSONString(commonOrderCancelRequest), JSONObject.toJSONString(response));
            // 调用的记录
            JdCallRecordPo jdCallRecordPo = new JdCallRecordPo();
            jdCallRecordPo.setWayBillCode(orderTransportPo.getWayBillCode());
            jdCallRecordPo.setOrderId(orderPo.getId());
            jdCallRecordPo.setOrderNo(orderPo.getOrderNo());
            jdCallRecordPo.setRequestInfo(JSONObject.toJSONString(commonOrderCancelRequest));
            jdCallRecordPo.setRespondInfo(JSONObject.toJSONString(response));
            jdCallRecordPo.setRequestId(response.getResult().getRequestId());
            jdCallRecordService.save(jdCallRecordPo);
            if (response.getResult().getCode() != 0) {
                log.error("取消jd的订单信息失败 jdCallRecordPo id 是{}", jdCallRecordPo.getId());
                throw new RuntimeException("订单取消失败,请联系管理员: " + orderTransportPo.getWayBillCode());
            }
            // 更新订单为取消
            orderPo.setStatus(OrderStatusEnum.CANCEL.getStatus());
            orderService.updateById(orderPo);
            // jd物流的信息,更新为取消
            orderTransportPo.setStatus("690");
            orderTransportService.updateById(orderTransportPo);

        } catch (LopException e) {
            throw new RuntimeException(e);
        }
        return 1;
    }
}
